package com.sanhe.a448.frag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.menu.app.db.bean.CusUsage;
import com.menu.app.db.bean.Custom;
import com.menu.app.fragment.FullScreenDlg;
import com.sanhe.a448.adapter.ParamAdapter;
import com.sanhe.a448.databinding.ParamDlgBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ParamDlg extends FullScreenDlg {

    View mExitView;
    ParamDlgBinding binding;
    List<CusUsage> data;
    Custom custom;
    boolean isAdmin;
    ParamAdapter adapter;

    public void setParam(List<CusUsage> data, Custom custom, boolean isAdmin) {
        this.data = data;
        this.custom = custom;
        this.isAdmin = isAdmin;
        if (this.data == null) {
            this.data = new ArrayList<>();
        } else {
            Iterator<CusUsage> it = this.data.iterator();
            while (it.hasNext()) {
                CusUsage u = it.next();
                if (u == null || u.info == null) {
                    it.remove();
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mExitView == null) {
            binding = ParamDlgBinding.inflate(inflater, container, false);
            mExitView = binding.getRoot();
            binding.close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss(false, null);
                }
            });
            binding.more.setOnClickListener(v -> {
                if (adapter != null) {
                    adapter.cancelDelete();
                }
            });
            adapter = new ParamAdapter(this, data, custom);
            binding.list.setLayoutManager(new LinearLayoutManager(getActivity()));
            binding.list.setAdapter(adapter);
        } else if (mExitView.getParent() instanceof ViewGroup) {
            ((ViewGroup) mExitView.getParent()).removeView(mExitView);
        }

        return mExitView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter != null && adapter.cancelDelete()) {
                    return;
                }
                if (isCancelable()) {
                    dismiss();
                }
            }
        });
    }

    public void toWork(CusUsage usage) {
        if (isAdmin) {
            return;
        }
        dismiss(true, usage);
    }

    public List<Long> getDelete() {
        return adapter != null ? adapter.getDelete() : Collections.emptyList();
    }
}
